package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.UserModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.function.BiConsumer;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//        System.out.println(22);
//        System.out.println(req.getMethod());
//        System.out.println(req.getContextPath());
//        System.out.println(req.getRequestURI());
//        System.out.println(req.getRequestURL());
//        System.out.println(req.getQueryString());
//        BufferedReader reader = req.getReader();
//        String s = reader.readLine();
//        System.out.println(s);
//        Enumeration<String> headerNames = req.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String s1 = headerNames.nextElement();
//            System.out.println(s1 + " = " + req.getHeaders(s1).nextElement());
////            System.out.println();
//        }
        Map<String, String[]> parameterMap = req.getParameterMap();
//        parameterMap.forEach(new BiConsumer<String, String[]>() {
//            @Override
//            public void accept(String s, String[] strings) {
//                System.out.println(s + "==" + Arrays.toString(strings));
//            }
//        });
//        parameterMap.forEach((aa, strings) -> System.out.println(aa + "==" + Arrays.toString(strings)));
//        Enumeration<String> connection = req.getHeaders("Connection");
//        String[] pass = req.getParameterValues("pass");
        String pass = req.getParameter("pass");
        System.out.println(pass);
        String name = req.getParameter("name");

        System.out.println(name);
        UserModel model = new UserModel(name, pass);
        String s = JSONObject.toJSONString(model);
        System.out.println(s);
//        如果前端传递了无数个参数，因为以后我们会对这些数据进行封装，
//        所以应该用一个大容器去接收
//        JavaBean：一般情况下如果这些数据意思差不多，属于一个对象
//        封装对象:以后，前端和后端发生交互的时候默认全部用对象
//        第四阶段用js取得Java后台数据通过如下代码进行
        resp.getWriter().println(s);
//        第三阶段：jsp，则将数据发送给前端用页面转发
    }

    public void aa(String name, String pass) {

    }
}
